<template>
	<view>
		<uni-forms ref="form">
			<view class="form_list">
				<view class="input" style="text-align: center;margin-top: 40rpx;">
					<image src="../../logo.png" class="logo"></image>
				</view>
			</view>
			<view class="form_list">
				<view class="input">
					<view>账号：</view>
					<uni-easyinput prefixIcon="person-filled" v-model="username" :placeholderStyle="placeholderStyle" :styles="styles"
					placeholder="请输入账号" trim="all"></uni-easyinput>
				</view>
			</view>
			<view class="form_list">
				<view class="input">
					<view>密码：</view>
					<uni-easyinput prefixIcon="locked-filled" type="password" v-model="password" :placeholderStyle="placeholderStyle" :styles="styles"
					placeholder="请输入密码" trim="all"></uni-easyinput>
				</view>
			</view>
			<!-- 仓库组件 -->
			<view class="form_list">
				<view class="input">
					<view>仓库：</view>
					<stockSelect :stock_id.sync="stock_id"></stockSelect>
				</view>
			</view>
			<view class="btn">
				<button @click="reset">重 置</button>
				<button @click="tologin">登 录</button>
			</view>
		</uni-forms>
	</view>
</template>

<script>
// 引入uni-ui组件
import {
  uniEasyinput,
  uniForms
} from "@dcloudio/uni-ui"
import stockSelect from "@/component/stockhouse.vue"
export default {
  data() {
    return {
	  placeholderStyle:'font-size:17px;',
	  styles:{
	  	borderColor: '#999'
	  },
	  username:'',
	  password:'',
	  stock_id:Vue.prototype.globalData.default_stock_id,//默认原料仓
	  stock_arr:[]
    }
  },
  components: {
	uniEasyinput,
	uniForms,
	stockSelect
  },
  watch:{
	  /* "stock_id":function(){
	  		console.log("markdown:"+this.stock_id) 
		} */
  },
  onLoad(){
	  if(!this.$Db.isOpen()){
	  	//打开数据库
	  	this.$Db.openSqlite()
	  }
	  
	  //判断是否有缓存，有则直接进首页
	   if(!uni.getStorageSync('user')){
	   	uni.reLaunch({
	   		url:'/pages/index/login',
	   		success() {
	   			plus.navigator.closeSplashscreen()}
	   	})
	   }else{
	   	uni.reLaunch({
	   		url:'/pages/index/index',
	   		success() {
	   			plus.navigator.closeSplashscreen()}
	   	})
	   }
	   
	   //监听登录结果
	   uni.$off("loginRes")
	   uni.$on("loginRes", (obj) => {
	   		 if(obj.result){
	   			 this.$Api.msg('登录成功','success')
				 uni.redirectTo({
				 	url:'/pages/index/index'
				 })
	   		 }else{
	   			 this.$Api.msg('登录失败,账号或密码错误')
	   		 }
	   })
	   
	   //用户表
	    this.$Db.queryIsTable('user').then(res => {
			if(!res){
				let data = '"userid" TEXT,"username" TEXT,"nickname" TEXT,"password" TEXT,"logintime" TEXT,"remark" TEXT';
				this.$Db.createTable('user',data)
			}
		})
		//仓库表
		this.$Db.queryIsTable('warehouse').then(res => {
			if(!res){
				let data = '"id" INTEGER,"name" TEXT,"number" TEXT,"remark" TEXT';
				this.$Db.createTable('warehouse',data)
			}
		})
  },
  methods: {
    reset(){
		this.username = ''
		this.password = ''
    },
    async tologin(){
		if(!this.username){
			this.$Api.msg('用户名不能为空');
			return
		}
		if(!this.password){
			this.$Api.msg('密码不能为空');
			return
		}
		
		if(this.username=='admin'&&this.password=='admin'){
			this.$Api.msg('登录成功','success')
			uni.setStorageSync('username','admin')
			uni.setStorageSync('user',null)
			//uni.clearStorageSync()
			uni.redirectTo({
				url:'/pages/config/set'
			})
		}else if(this.username=='admin'){
			this.$Api.msg('密码错误','error')
		}else{
			if(uni.getStorageSync('myconfig')==''){
				this.$Api.msg('请先初始化','error')
				return
			}
			
			uni.showLoading();
			this.$Db.toLogin(this.username,this.password,this.stock_id)
			.then(res=>{
				uni.hideLoading()
			})
			.catch(err=>{
				console.log(err)
			})
		}
    },
  }
}
</script>